<?php
/**
 * CakeCMS - 内容管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class ContentController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'AdminLog' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS内容管理' );
	}
	public function index() {
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
	}
	/**
	 * 管理栏目
	 */
	public function category() {
		$this->set ( 'title_for_layout', '栏目管理' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'category.js' 
		) );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$tree->icon = array (
				'&nbsp;&nbsp;&nbsp;│ ',
				'&nbsp;&nbsp;&nbsp;├─ ',
				'&nbsp;&nbsp;&nbsp;└─ ' 
		);
		$tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$this->loadModel ( "Category" );
		
		$conditions = array ();
		$result = $this->Category->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'listorder ASC',
						'id ASC' 
				) 
		) );
		
		Configure::load ( "config" );
		$admin_config = Configure::read ( 'admin_config' );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['Category'] ['id'];
			$r ['parentid'] = $val ['Category'] ['parentid'];
			$r ['name'] = $val ['Category'] ['name'];
			$r ['description'] = $val ['Category'] ['description'];
			$r ['controller'] = $val ['Category'] ['controller'];
			$r ['listorder'] = $val ['Category'] ['listorder'];
			$r ['ismenu'] = $val ['Category'] ['ismenu'];
			$r ['hits'] = $val ['Category'] ['hits'];
			
			$r ['controller'] = "<a target=\"_blank\" href=\"{$admin_config['mainDomain']}{$val['Category']['controller']}\">访问</a>";
			if ($val ['Category'] ['ismenu'] == 1) {
				$r ['display_icon'] = '';
			} else {
				$r ['display_icon'] = "<img title=\"不在导航显示\" src=\"/img/icon/gear_disable.png\">";
			}
			$r ['str_manage'] = '<a href="/Content/addCate/' . $val ['Category'] ['id'] . '/">添加子菜单</a> | <a href="/Content/editCate/' . $val ['Category'] ['id'] . '/">修改</a> | <a href="javascript:;" onclick="delete_cate(' . $val ['Category'] ['id'] . ');">删除</a> ';
			
			$array [] = $r;
		}
		
		$str = "<tr>
					<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
					<td align='center'>\$id</td>
					<td >\$spacer\$name\$display_icon</td>
					<td align='center'>\$hits</td>
					<td align='center'>\$controller</td>
					<td align='center' >\$str_manage</td>
				</tr>";
		
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		$this->set ( 'categorys', $select_categorys );
	}
	/**
	 * 添加栏目
	 */
	public function addCate() {
		if ($this->request->isPost ()) {
			if (! isset ( $this->data ['info'] ['parentid'] )) {
				$this->flash ( '请选择上级栏目!', '/Content/addCate/', 1250 );
				return false;
			}
			if (empty ( $this->data ['info'] ['name'] )) {
				$this->flash ( '栏目名称不能为空!', '/Content/addCate/', 1250 );
				return false;
			}
			if (empty ( $this->data ['info'] ['controller'] )) {
				$this->flash ( '栏目地址不能为空!', '/Content/addCate/', 1250 );
				return false;
			}
			if (! isset ( $this->data ['info'] ['ismenu'] )) {
				$this->flash ( '请选择是否在导航显示!', '/Content/addCate/', 1250 );
				return false;
			}
			$data ['Category'] = $this->data ['info'];
			$setting = $this->data ['setting'];
			$data ['Category'] ['setting'] = serialize ( $setting );
			
			$this->loadModel ( "Category" );
			if ($this->Category->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加栏目!' );
				
				$this->flash ( '添加栏目成功!', '/Content/category/', 1250 );
				return false;
			} else {
				$this->flash ( '添加栏目失败!', '/Content/addCate/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '添加栏目' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'category.js' 
			) );
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "Category" );
			
			$parentid = $this->params ['parentid'];
			
			$conditions = array ();
			$result = $this->Category->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'listorder ASC',
							'id ASC' 
					) 
			) );
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['Category'] ['id'];
				$r ['parentid'] = $val ['Category'] ['parentid'];
				$r ['name'] = $val ['Category'] ['name'];
				$r ['description'] = $val ['Category'] ['description'];
				$r ['controller'] = $val ['Category'] ['controller'];
				$r ['listorder'] = $val ['Category'] ['listorder'];
				$r ['ismenu'] = $val ['Category'] ['ismenu'];
				$r ['hits'] = $val ['Category'] ['hits'];
				
				$r ['selected'] = $val ['Category'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$this->set ( 'select_categorys', $select_categorys );
		}
	}
	/**
	 * 编辑栏目
	 */
	public function editCate() {
		if ($this->request->isPost ()) {
			
			$id = $this->data ['id'];
			if (empty ( $id )) {
				$this->flash ( '参数错误!', "/Content/category/", 1250 );
				return false;
			}
			
			if (! isset ( $this->data ['info'] ['parentid'] )) {
				$this->flash ( '请选择上级栏目!', "/Content/editCate/{$id}/", 1250 );
				return false;
			}
			if (empty ( $this->data ['info'] ['name'] )) {
				$this->flash ( '栏目名称不能为空!', '/Content/editCate/{$id}/', 1250 );
				return false;
			}
			if (empty ( $this->data ['info'] ['controller'] )) {
				$this->flash ( '栏目地址不能为空!', '/Content/editCate/{$id}/', 1250 );
				return false;
			}
			if (! isset ( $this->data ['info'] ['ismenu'] )) {
				$this->flash ( '请选择是否在导航显示!', '/Content/editCate/{$id}/', 1250 );
				return false;
			}
			$data ['Category'] = $this->data ['info'];
			$setting = $this->data ['setting'];
			$data ['Category'] ['setting'] = serialize ( $setting );
			
			$data ['Category'] ['id'] = $id;
			
			$this->loadModel ( "Category" );
			if ($this->Category->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '编辑栏目,栏目ID:' . $id );
				
				$this->flash ( '编辑栏目成功!', '/Content/category/', 1250 );
				return false;
			} else {
				$this->flash ( '编辑栏目失败!', '/Content/editCate/{$id}/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '编辑栏目' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'category.js' 
			) );
			
			$id = $this->params ['id'];
			
			// 通用的树型类
			App::import ( 'Lib', 'Tree', array (
					'file' => '/Lib/Tree.php' 
			) );
			
			$tree = new Tree ();
			$this->loadModel ( "Category" );
			
			$conditions = array ();
			$result = $this->Category->find ( 'all', array (
					'conditions' => $conditions,
					'order' => array (
							'listorder ASC',
							'id ASC' 
					) 
			) );
			
			$parentid_Info = $this->Category->GetCateByID ( $id );
			$parentid = $parentid_Info ['Category'] ['parentid'];
			
			$array = array ();
			foreach ( $result as $val ) {
				$r ['id'] = $val ['Category'] ['id'];
				$r ['parentid'] = $val ['Category'] ['parentid'];
				$r ['name'] = $val ['Category'] ['name'];
				$r ['description'] = $val ['Category'] ['description'];
				$r ['controller'] = $val ['Category'] ['controller'];
				$r ['listorder'] = $val ['Category'] ['listorder'];
				$r ['ismenu'] = $val ['Category'] ['ismenu'];
				$r ['hits'] = $val ['Category'] ['hits'];
				$r ['selected'] = $val ['Category'] ['id'] == $parentid ? 'selected' : '';
				$array [] = $r;
			}
			$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init ( $array );
			$select_categorys = $tree->get_tree ( 0, $str );
			
			$Cate_info = $this->Category->GetCateByID ( $id );
			$Cate_info ['Category'] ['setting'] = unserialize ( $Cate_info ['Category'] ['setting'] );
			$this->set ( 'Cate_info', $Cate_info );
			$this->set ( 'select_categorys', $select_categorys );
			$this->set ( 'id', $id );
		}
	}
	/**
	 * 删除栏目
	 */
	public function delCate() {
		$id = $this->data ['id'];
		
		if (empty ( $id )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '参数不能为空,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Category" );
		if ($this->Category->del ( $id )) {
			// 记录操作日志
			$this->AdminLog->addLog ( '删除栏目,栏目ID:' . $id );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
	
	/**
	 * 排序
	 */
	public function listorder() {
		if ($this->request->isPost ()) {
			$listorders = $this->data ['listorders'];
			if (is_array ( $listorders )) {
				$this->loadModel ( "Category" );
				foreach ( $listorders as $id => $listorder ) {
					$data ['Category'] ['listorder'] = $listorder;
					$data ['Category'] ['id'] = $id;
					$this->Category->edit ( $data );
				}
				$this->flash ( '操作成功!', '/Content/category/', 1250 );
				return false;
			} else {
				$this->flash ( '操作失败!', '/Content/category/', 1250 );
				return false;
			}
		} else {
			$this->flash ( '操作失败!', '/Content/category/', 1250 );
			return false;
		}
	}
	/**
	 * 左侧菜单
	 */
	public function left() {
		$this->set ( 'title_for_layout', '左侧菜单' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'jquery.treeview.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'jquery.cookie.js',
				'jquery.treeview.js',
				'jquery.treeview.async.js' 
		) );
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		// 这块配置子菜单是否ajax取的
		$ajax_show = 0; // 0:为默认加载输出 1:为AJAX动态加载输出
		$tree = new Tree ();
		
		$this->loadModel ( "Category" );
		
		$conditions = array ();
		$result = $this->Category->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'listorder ASC',
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['Category'] ['id'];
			$r ['parentid'] = $val ['Category'] ['parentid'];
			$r ['name'] = $val ['Category'] ['name'];
			$r ['description'] = $val ['Category'] ['description'];
			$r ['controller'] = $val ['Category'] ['controller'];
			$r ['listorder'] = $val ['Category'] ['listorder'];
			$r ['ismenu'] = $val ['Category'] ['ismenu'];
			$r ['hits'] = $val ['Category'] ['hits'];
			$r ['icon_type'] = "";
			$r ['add_icon'] = " <a href=\"/Content/manage/{$val['Category']['id']}/\" target=\"right\"><img alt=\"添加\" src=\"/img/add_content.gif\"></a> ";
			
			$array [] = $r;
		}
		
		$strs = "<span class='\$icon_type'>\$add_icon<a href='/Content/manage/\$id/' target='right' onclick='open_list(this)'>\$name</a></span>";
		$strs2 = "<span class='folder'>\$name</span>";
		$tree->init ( $array );
		$categorys = $tree->get_treeview ( 0, 'category_tree', $strs, $strs2, $ajax_show );
		
		$this->set ( 'ajax_show', $ajax_show );
		$this->set ( 'categorys', $categorys );
	}
	/**
	 * 内容管理列表
	 */
	public function manage() {
		$this->set ( 'title_for_layout', '管理内容' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js',
				'jquery.cookie.js',
				'jquery.imagePreview.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$cid = $this->params ['cid'];
		
		$this->loadModel ( "News" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$posids = isset ( $this->params ['named'] ['posids'] ) ? $this->params ['named'] ['posids'] : '';
		$searchtype = isset ( $this->params ['named'] ['searchtype'] ) ? $this->params ['named'] ['searchtype'] : '';
		$keyword = isset ( $this->params ['named'] ['keyword'] ) ? $this->params ['named'] ['keyword'] : '';
		$start_time = isset ( $this->params ['named'] ['start_time'] ) ? $this->params ['named'] ['start_time'] : '';
		$end_time = isset ( $this->params ['named'] ['end_time'] ) ? $this->params ['named'] ['end_time'] : '';
		
		// 整理搜索条件
		$conditions = array (
				'catid' => $cid 
		);
		
		if (! empty ( $posids )) {
			$conditions ['istop'] = $posids;
		}
		if (! empty ( $keyword )) {
			if ($searchtype == 1) {
				$conditions ['title'] = "LIKE %$keyword%";
			} elseif ($searchtype == 2) {
				$conditions ['description'] = "LIKE %$keyword%";
			} elseif ($searchtype == 3) {
				$conditions ['username'] = "LIKE %$keyword%";
			} elseif ($searchtype == 4) {
				$conditions ['id'] = $keyword;
			}
		}
		if (! empty ( $start_time )) {
			$conditions ['updatetime >='] = $start_time;
		}
		if (! empty ( $end_time )) {
			$conditions ['updatetime <='] = $end_time;
		}
		
		$list = $this->News->GetNewsByList ( $conditions, $limit );
		
		if ($list) {
			foreach ( $list ['result'] as &$val ) {
				if (! empty ( $val ['News'] ['style'] )) {
					$style = explode ( ';', $val ['News'] ['style'] );
					$val ['News'] ['style'] = '';
					if (! empty ( $style [0] ) && strstr ( $style [0], '#' )) {
						$val ['News'] ['style'] .= "color:{$style[0]};";
					}
					if (! empty ( $style [0] ) && strstr ( $style [0], 'bold' )) {
						$val ['News'] ['style'] .= "font-weight:{$style[0]};";
					}
					if (! empty ( $style [1] ) && strstr ( $style [1], '#' )) {
						$val ['News'] ['style'] .= "color:{$style[1]};";
					}
					if (! empty ( $style [1] ) && strstr ( $style [1], 'bold' )) {
						$val ['News'] ['style'] .= "font-weight:{$style[1]};";
					}
				}
			}
		}
		
		$this->set ( 'cid', $cid );
		
		$this->set ( 'posids', $posids );
		$this->set ( 'searchtype', $searchtype );
		$this->set ( 'keyword', $keyword );
		$this->set ( 'start_time', $start_time );
		$this->set ( 'end_time', $end_time );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 获取关键词
	 */
	public function keywords() {
		$this->autoRender = false;
	}
	/**
	 * 添加内容
	 */
	public function addContent() {
		if ($this->request->isPost ()) {
			
			$catid = $this->data ['catid'];
			if (empty ( $catid )) {
				$this->flash ( '栏目参数错误!', "/Content/addContent/{$catid}/", 1250 );
				return false;
			}
			$data ['News'] ['catid'] = $catid;
			
			if (empty ( $this->data ['title'] )) {
				$this->flash ( '标题不能为空!', "/Content/addContent/{$catid}/", 1250 );
				return false;
			}
			$data ['News'] ['title'] = $this->data ['title'];
			
			$style = '';
			if (! empty ( $this->data ['style_color'] )) {
				$style = $this->data ['style_color'];
			}
			if (! empty ( $this->data ['style_font_weight'] )) {
				$style .= ';' . $this->data ['style_font_weight'];
			}
			
			$data ['News'] ['style'] = $style;
			$data ['News'] ['news_img'] = $this->data ['news_img'];
			$data ['News'] ['keywords'] = $this->data ['keywords'];
			$data ['News'] ['description'] = $this->data ['description'];
			$data ['News'] ['istop'] = $this->data ['istop'];
			$data ['News'] ['status'] = $this->data ['status'];
			$data ['News'] ['hits'] = 0;
			$data ['News'] ['username'] = $this->Session->read ( 'username' );
			$data ['News'] ['listorder'] = 0;
			
			if (! empty ( $this->data ['createtime'] )) {
				$createtime = $this->data ['createtime'];
			} else {
				$createtime = date ( "Y-m-d H:i:s", time () );
			}
			$data ['News'] ['createtime'] = $createtime;
			$data ['News'] ['updatetime'] = date ( "Y-m-d H:i:s", time () );
			
			$this->loadModel ( "News" );
			$NewsInfo = $this->News->save ( $data );
			
			if ($NewsInfo && $NewsInfo ['News'] ['id'] > 0) {
				$news_data ['NewsData'] ['id'] = $NewsInfo ['News'] ['id'];
				$news_data ['NewsData'] ['content'] = $this->data ['content'];
				$news_data ['NewsData'] ['pagetype'] = $this->data ['pagetype'];
				$news_data ['NewsData'] ['maxcharperpage'] = $this->data ['maxcharperpage'];
				$news_data ['NewsData'] ['relation'] = $this->data ['relation'];
				$news_data ['NewsData'] ['allow_comment'] = $this->data ['allow_comment'];
				$news_data ['NewsData'] ['copyfrom'] = $this->data ['copyfrom'];
				
				$this->loadModel ( "NewsData" );
				$NewsDataInfo = $this->NewsData->save ( $news_data );
				
				if ($NewsDataInfo && is_array ( $NewsDataInfo )) {
					if (isset ( $this->data ['dosubmit'] )) {
						$this->flash ( '发布成功！', "/blank/", 1250 );
						return false;
					}
					
					if (isset ( $this->data ['dosubmit_continue'] )) {
						$this->flash ( '发布成功！!', "/Content/addContent/{$catid}/", 1250 );
						return false;
					}
				} else {
					$this->flash ( '发布内容失败！!', "/Content/addContent/{$catid}/", 1250 );
					return false;
				}
			} else {
				$this->flash ( '发布内容失败！!', "/Content/addContent/{$catid}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '添加内容' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'calendar/jscal2.css',
					'calendar/border-radius.css',
					'calendar/win2k.css',
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'calendar/calendar.js',
					'calendar/lang/en.js',
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'content.js' 
			) );
			
			$catid = $this->params ['cid'];
			
			$this->loadModel ( "Category" );
			$Cate_info = $this->Category->GetCateByID ( $catid );
			$this->set ( 'Cate_info', $Cate_info );
			
			$this->set ( 'catid', $catid );
			$this->set ( 'today', date ( "Y-m-d H:i:s", time () ) );
			
			// 来源
			$this->set ( 'copyfrom', Configure::read ( 'admin_config.copyfrom' ) );
		}
	}
	/**
	 * 编辑内容
	 */
	public function editContent() {
		if ($this->request->isPost ()) {
			$catid = $this->data ['catid'];
			$id = $this->data ['id'];
			
			if (empty ( $catid ) || empty ( $id )) {
				$this->flash ( '栏目参数错误!', "/Content/addContent/{$catid}/{$id}/", 1250 );
				return false;
			}
			$data ['News'] ['catid'] = $catid;
			
			if (empty ( $this->data ['title'] )) {
				$this->flash ( '标题不能为空!', "/Content/addContent/{$catid}/{$id}/", 1250 );
				return false;
			}
			$data ['News'] ['title'] = $this->data ['title'];
			
			$style = '';
			if (! empty ( $this->data ['style_color'] )) {
				$style = $this->data ['style_color'];
			}
			if (! empty ( $this->data ['style_font_weight'] )) {
				$style .= ';' . $this->data ['style_font_weight'];
			}
			
			$data ['News'] ['style'] = $style;
			$data ['News'] ['news_img'] = $this->data ['news_img'];
			$data ['News'] ['keywords'] = $this->data ['keywords'];
			$data ['News'] ['description'] = $this->data ['description'];
			$data ['News'] ['istop'] = $this->data ['istop'];
			$data ['News'] ['status'] = $this->data ['status'];
			
			if (! empty ( $this->data ['createtime'] )) {
				$createtime = $this->data ['createtime'];
			} else {
				$createtime = date ( "Y-m-d H:i:s", time () );
			}
			$data ['News'] ['createtime'] = $createtime;
			$data ['News'] ['updatetime'] = date ( "Y-m-d H:i:s", time () );
			
			$this->loadModel ( "News" );
			$data ['News'] ['id'] = $id;
			$return = $this->News->edit ( $data );
			
			if ($return) {
				$news_data ['NewsData'] ['content'] = $this->data ['content'];
				$news_data ['NewsData'] ['pagetype'] = $this->data ['pagetype'];
				$news_data ['NewsData'] ['maxcharperpage'] = $this->data ['maxcharperpage'];
				$news_data ['NewsData'] ['relation'] = $this->data ['relation'];
				$news_data ['NewsData'] ['allow_comment'] = $this->data ['allow_comment'];
				$news_data ['NewsData'] ['copyfrom'] = $this->data ['copyfrom'];
				
				$this->loadModel ( "NewsData" );
				$news_data ['NewsData'] ['id'] = $id;
				
				if ($this->NewsData->edit ( $news_data )) {
					if (isset ( $this->data ['dosubmit'] )) {
						$this->flash ( '编辑成功！', "/blank/", 1250 );
						return false;
					}
					
					if (isset ( $this->data ['dosubmit_continue'] )) {
						$this->flash ( '编辑成功！!', "/Content/addContent/{$catid}/{$id}/", 1250 );
						return false;
					}
				} else {
					$this->flash ( '编辑失败！!', "/Content/addContent/{$catid}/{$id}/", 1250 );
					return false;
				}
			} else {
				$this->flash ( '发布内容失败！!', "/Content/addContent/{$catid}/{$id}/", 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '编辑内容' );
			
			// 加载css文件
			$this->set ( 'cssList', array (
					'calendar/jscal2.css',
					'calendar/border-radius.css',
					'calendar/win2k.css',
					'kindeditor/themes/default/default.css',
					'kindeditor/plugins/code/prettify.css' 
			) );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'calendar/calendar.js',
					'calendar/lang/en.js',
					'jquery.cookie.js',
					'colorpicker.js',
					'kindeditor/kindeditor.js',
					'kindeditor/lang/zh_CN.js',
					'kindeditor/plugins/code/prettify.js',
					'content.js' 
			) );
			
			$catid = $this->params ['cid'];
			$id = $this->params ['id'];
			
			$this->loadModel ( "Category" );
			$Cate_info = $this->Category->GetCateByID ( $catid );
			$this->set ( 'Cate_info', $Cate_info );
			
			$this->loadModel ( "News" );
			$NewsInfo = $this->News->GetNewsByID ( $id );
			if ($NewsInfo) {
				$relation = explode ( '|', $NewsInfo ['News'] ['relation'] );
				if (is_array ( $relation )) {
					$NewsInfo ['News'] ['relation_info'] = array ();
					foreach ( $relation as $val ) {
						$Info = $this->News->GetNewsByID ( $val );
						array_push ( $NewsInfo ['News'] ['relation_info'], array (
								'id' => $Info ['News'] ['id'],
								'title' => cutstr ( $Info ['News'] ['title'], 24, '...' ) 
						) );
					}
				}
			}
			
			$this->set ( 'NewsInfo', $NewsInfo );
			
			// 来源
			$this->set ( 'copyfrom', Configure::read ( 'admin_config.copyfrom' ) );
			
			$this->set ( 'catid', $catid );
			$this->set ( 'id', $id );
		}
	}
	/**
	 * 添加相关文章
	 */
	public function relationlist() {
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$cid = $this->params ['cid'];
		
		// 通用的树型类
		App::import ( 'Lib', 'Tree', array (
				'file' => '/Lib/Tree.php' 
		) );
		
		$tree = new Tree ();
		$this->loadModel ( "Category" );
		
		$conditions = array ();
		$result = $this->Category->find ( 'all', array (
				'conditions' => $conditions,
				'order' => array (
						'listorder ASC',
						'id ASC' 
				) 
		) );
		
		$array = array ();
		foreach ( $result as $val ) {
			$r ['id'] = $val ['Category'] ['id'];
			$r ['parentid'] = $val ['Category'] ['parentid'];
			$r ['name'] = $val ['Category'] ['name'];
			$r ['description'] = $val ['Category'] ['description'];
			$r ['controller'] = $val ['Category'] ['controller'];
			$r ['listorder'] = $val ['Category'] ['listorder'];
			$r ['ismenu'] = $val ['Category'] ['ismenu'];
			$r ['hits'] = $val ['Category'] ['hits'];
			$r ['selected'] = $val ['Category'] ['id'] == '';
			$array [] = $r;
		}
		$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init ( $array );
		$select_categorys = $tree->get_tree ( 0, $str );
		
		$this->set ( 'select_categorys', $select_categorys );
		
		$this->loadModel ( "News" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$field = isset ( $this->params ['named'] ['field'] ) ? $this->params ['named'] ['field'] : '';
		$catid = isset ( $this->params ['named'] ['catid'] ) ? $this->params ['named'] ['catid'] : 0;
		$keyword = isset ( $this->params ['named'] ['keyword'] ) ? $this->params ['named'] ['keyword'] : '';
		
		// 整理搜索条件
		if (! empty ( $catid )) {
			$conditions = array (
					'catid' => $catid 
			);
		}
		if (! empty ( $keyword )) {
			if ($field == 'title') {
				$conditions ['title'] = "LIKE %$keyword%";
			} elseif ($field == 'keywords') {
				$conditions ['description'] = "LIKE %$keyword%";
			} elseif ($field == 'description') {
				$conditions ['description'] = "LIKE %$keyword%";
			} elseif ($field == 'id') {
				$conditions ['id'] = $keyword;
			}
		}
		
		$list = $this->News->GetNewsByList ( $conditions, $limit );
		
		if ($list) {
			foreach ( $list ['result'] as &$val ) {
				$catInfo = $this->Category->GetCateByID ( $val ['News'] ['catid'] );
				$val ['News'] ['catname'] = $catInfo ['Category'] ['name'];
				
				if (! empty ( $val ['News'] ['style'] )) {
					$style = explode ( ';', $val ['News'] ['style'] );
					$val ['News'] ['style'] = '';
					if (! empty ( $style [0] ) && strstr ( $style [0], '#' )) {
						$val ['News'] ['style'] .= "color:{$style[0]};";
					}
					if (! empty ( $style [0] ) && strstr ( $style [0], 'bold' )) {
						$val ['News'] ['style'] .= "font-weight:{$style[0]};";
					}
					if (! empty ( $style [1] ) && strstr ( $style [1], '#' )) {
						$val ['News'] ['style'] .= "color:{$style[1]};";
					}
					if (! empty ( $style [1] ) && strstr ( $style [1], 'bold' )) {
						$val ['News'] ['style'] .= "font-weight:{$style[1]};";
					}
				}
			}
		}
		
		$this->set ( 'cid', $cid );
		
		$this->set ( 'catid', $catid );
		$this->set ( 'field', $field );
		$this->set ( 'keyword', $keyword );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 推送
	 */
	public function push() {
	}
	/**
	 * 批量移动
	 */
	public function remove() {
	}
	/**
	 * 查看评论
	 */
	public function comment() {
		$this->set ( 'title_for_layout', '查看评论' );
		
		$id = $this->params ['id'];
		
		$this->loadModel ( "Comment" );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$searchtype = isset ( $this->params ['named'] ['searchtype'] ) ? $this->params ['named'] ['searchtype'] : '';
		$keyword = isset ( $this->params ['named'] ['keyword'] ) ? $this->params ['named'] ['keyword'] : '';
		
		// 整理搜索条件
		$conditions = array (
				'id' => $id 
		);
		
		if (! empty ( $searchtype )) {
			$conditions ['updatetime >='] = $searchtype;
		}
		if (! empty ( $keyword )) {
			$conditions ['updatetime <='] = $keyword;
		}
		
		$list = $this->Comment->GetCommentByList ( $conditions, $limit );
		if ($list) {
			$this->loadModel ( "News" );
			$this->loadModel ( "User" );
			
			foreach ( $list ['result'] as &$val ) {
				$UserInfo = $this->User->GetUserByID ( $val ['Comment'] ['uid'] );
				$val ['Comment'] ['User'] ['uid'] = $UserInfo ['User'] ['uid'];
				$val ['Comment'] ['User'] ['real_name'] = $UserInfo ['User'] ['detail'] ['real_name'];
				
				$NewsInfo = $this->News->GetNewsByID ( $val ['Comment'] ['id'] );
				$val ['Comment'] ['News'] ['id'] = $NewsInfo ['News'] ['id'];
				$val ['Comment'] ['News'] ['title'] = $NewsInfo ['News'] ['title'];
			}
		}
		
		$this->set ( 'id', $id );
		$this->set ( 'searchtype', $searchtype );
		$this->set ( 'keyword', $keyword );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	/**
	 * 删除评论
	 */
	public function delComment() {
		$this->autoRender = false;
		if ($this->request->isPost ()) {
			$id = $this->data ['id'];
			$ids = explode ( ',', $id );
			if (is_array ( $ids )) {
				$this->loadModel ( "Comment" );
				foreach ( $ids as $id ) {
					$this->Comment->del ( $id );
				}
				exit ( '1' );
			}
		} else {
			exit ( '-1' );
		}
	}
	/**
	 * 删除内容
	 */
	public function delete() {
		$this->autoRender = false;
		
		if ($this->request->isPost ()) {
			$ids = $this->data ['ids'];
			$catid = $this->data ['catid'];
			
			if (is_array ( $ids )) {
				foreach ( $ids as $id ) {
					$this->loadModel ( "NewsData" );
					$return = $this->NewsData->del ( $id );
					if ($return) {
						$this->loadModel ( "News" );
						$this->News->del ( $id );
						
						$this->flash ( '删除成功！!', "/Content/manage/{$catid}/", 1250 );
						return false;
					}
				}
			}
		}
	}
}
?>